/**
 * Copyright © 2021 易久批信息技术有限公司. All rights reserved.
 */
package com.dysm.sort;

import java.util.Arrays;

/**
 * 动图演示链接：https://images2017.cnblogs.com/blog/849589/201710/849589-20171015224719590-1433219824.gif
 * 选择排序：外层一次循环，数组中最小值位于数组头部
 * @author xiay
 * @since 2021/1/30 10:58 上午
 */
public class SelectSortTest {

    public static void main(String[] args) {
        int[] arr = {9,5,8,3,2,4,6,7};
        System.out.println(Arrays.toString(arr));

        selectSort(arr);

        System.out.println(Arrays.toString(arr));
    }

    private static void selectSort(int[] arr) {
        int length = arr.length;
        for (int i = 0; i < length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < length; j++) {
                if (arr[minIndex] > arr[j]) {
                    minIndex = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }


}
